package cn.seecoder.courselearning.serviceimpl.course;

import cn.seecoder.courselearning.dto.course.CourseQuestionDTO;
import cn.seecoder.courselearning.dto.course.CourseTestDTO;
import cn.seecoder.courselearning.dto.course.CourseTestFinishDTO;
import cn.seecoder.courselearning.dto.course.CourseTestQuestionAddDTO;
import cn.seecoder.courselearning.mapperservice.course.CourseMapper;
import cn.seecoder.courselearning.mapperservice.course.test.*;
import cn.seecoder.courselearning.po.course.*;
import cn.seecoder.courselearning.po.course.test.*;
import cn.seecoder.courselearning.service.course.CourseTestService;
import cn.seecoder.courselearning.vo.ResultVO;
import cn.seecoder.courselearning.vo.course.test.CourseQuestionVO;
import cn.seecoder.courselearning.vo.course.test.CourseTestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CourseTestServiceImpl implements CourseTestService {

    @Autowired
    CourseQuestionMapper courseQuestionMapper;

    @Autowired
    CourseTestMapper courseTestMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    CourseUserTestMapper courseUserTestMapper;

    @Autowired
    CourseTestQuestionMapper courseTestQuestionMapper;

    @Autowired
    CourseUserTestQuestionMapper courseUserTestQuestionMapper;

    @Override
    public ResultVO createQuestion(CourseQuestionDTO courseQuestionDTO) {
        courseQuestionMapper.createQuestion(new CourseQuestionPO(courseQuestionDTO));
        return ResultVO.succeed();
    }

    @Override
    public ResultVO checkAnswerAndCalculateScore(CourseTestFinishDTO courseTestFinishDTO){
        List<CourseUserTestQuestionPO> courseUserTestQuestionPOS=new ArrayList<>();
        int correctNumber=0;
        for (Map.Entry<Integer, String> entry : courseTestFinishDTO.getQuestionIdAnswers().entrySet()) {
            // key是questionId
            // value是学生的answer
            int questionId=entry.getKey();
            String answer=entry.getValue();
            //根据questionId获得题目信息
            CourseQuestionPO courseQuestionPO=courseQuestionMapper.selectQuestionByQuestionId(questionId);
            if(courseQuestionPO.getAnswer().equals(answer)){
               CourseUserTestQuestionPO courseUserTestQuestionPO=new CourseUserTestQuestionPO(courseTestFinishDTO.getUserId(),entry.getKey(), courseTestFinishDTO.getTestId(), entry.getValue(),1);
               courseUserTestQuestionPOS.add(courseUserTestQuestionPO);
               correctNumber++;
            }
            else{
                CourseUserTestQuestionPO courseUserTestQuestionPO=new CourseUserTestQuestionPO( courseTestFinishDTO.getUserId(),entry.getKey(), courseTestFinishDTO.getTestId(), entry.getValue(),0);
                courseUserTestQuestionPOS.add(courseUserTestQuestionPO);
            }
        }

        courseUserTestQuestionMapper.updateTestQuestions(courseUserTestQuestionPOS);
        int questionNumber=courseUserTestQuestionPOS.size();
        int score=(int)((1.0*correctNumber/questionNumber)*100);
        courseUserTestMapper.updateTest(new CourseUserTestPO(courseTestFinishDTO.getUserId(),courseTestFinishDTO.getTestId(),score));
        return ResultVO.succeed();
    }

    @Override
    public ResultVO<List<CourseQuestionVO>> getQuestionsByCourseId(int courseId) {
        List<CourseQuestionPO> courseQuestionPOList = courseQuestionMapper.selectQuestionByCourseId(courseId);
        List<CourseQuestionVO> courseQuestionVOList = new ArrayList<>();
        for (CourseQuestionPO courseQuestionPO : courseQuestionPOList) {
            courseQuestionVOList.add(new CourseQuestionVO(courseQuestionPO));
        }
        return new ResultVO<>(courseQuestionVOList);
    }

    @Override
    public ResultVO<List<CourseQuestionVO>> getQuestionsByTestId(int testId) {
        List<CourseQuestionPO> courseQuestionPOList = courseQuestionMapper.selectQuestionByTestId(testId);
        List<CourseQuestionVO> courseQuestionVOList = new ArrayList<>();
        for (CourseQuestionPO courseQuestionPO : courseQuestionPOList) {
            courseQuestionVOList.add(new CourseQuestionVO(courseQuestionPO));
        }
        return new ResultVO<>(courseQuestionVOList);
    }

    @Override
    public ResultVO fixQuestion(CourseQuestionDTO courseQuestionDTO) {
        courseQuestionMapper.updateQuestion (new CourseQuestionPO(courseQuestionDTO));
        return ResultVO.succeed();
    }

    @Override
    public ResultVO deleteQuestion(CourseQuestionDTO courseQuestionDTO) {
        courseQuestionMapper.deleteQuestion(courseQuestionDTO.getQuestionId());
        return ResultVO.succeed();
    }

    @Override
    public ResultVO deleteTest(CourseTestDTO courseTestDTO) {
        courseTestMapper.deleteTest(courseTestDTO.getTestId());
        return ResultVO.succeed();
    }

    @Override
    public ResultVO fixTest(CourseTestDTO courseTestDTO) {
        courseTestMapper.updateTest(new CourseTestPO(courseTestDTO));
        return ResultVO.succeed();
    }

    @Override
    public ResultVO createTest(CourseTestDTO courseTestDTO) {
        courseTestMapper.createTest(new CourseTestPO(courseTestDTO));
        return ResultVO.succeed();
    }

    @Override
    public ResultVO<List<CourseTestVO>> getTestsByCourseId(int courseId) {
        List<CourseTestPO> courseTestPOList = courseTestMapper.findAllTest(courseId);
        List<CourseTestVO> courseTestVOList = new ArrayList<>();
        for (CourseTestPO courseTestPO : courseTestPOList) {
            courseTestVOList.add(new CourseTestVO(courseTestPO));
        }
        return new ResultVO<>(courseTestVOList);
    }

    @Override
    public ResultVO<List<CourseTestVO>> getTestsByTeacherId(int teacherId) {
        List<Course> courseList = courseMapper.selectByTeacherId(teacherId);
        List<CourseTestPO> courseTestPOList = new ArrayList<>();
        List<CourseTestVO> courseTestVOList = new ArrayList<>();
        for (Course course : courseList) {
            courseTestPOList.addAll(courseTestMapper.findAllTest(course.getId()));
        }
        for (CourseTestPO courseTestPO : courseTestPOList) {
            courseTestVOList.add(new CourseTestVO(courseTestPO));
        }
        return new ResultVO<>(courseTestVOList);
    }

    @Override
    public ResultVO finishTest(CourseTestFinishDTO courseTestFinishDTO) {
        if (courseTestFinishDTO.isFinished()) {
            checkAnswerAndCalculateScore(courseTestFinishDTO);
        } else {
            courseUserTestMapper.finishTest(new CourseUserTestPO(courseTestFinishDTO));
            courseUserTestQuestionMapper.finishTestQuestions(CourseUserTestQuestionPO.transferFromDTO(courseTestFinishDTO));
        }
        return ResultVO.succeed();
    }

    @Override
    public ResultVO addTestQuestion(CourseTestQuestionAddDTO courseTestQuestionAddDTO) {
        courseTestQuestionMapper.addTestQuestion(CourseTestQuestionPO.transferFromDTO(courseTestQuestionAddDTO));
        return ResultVO.succeed();
    }
}
